package com.xinzhi.savingpot.util;

/**
 * swf
 * 2021/01/13——09:44:10
 */

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayFundTransToaccountTransferRequest;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.response.AlipayFundTransToaccountTransferResponse;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import com.xinzhi.savingpot.configure.ALipayConfig;
import com.xinzhi.savingpot.rminfo.pojo.KryptonVO;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * 充值提现
 */
public class ALiPayUtil {

    /**
     * 支付宝支付工具类
     */
    ALipayConfig alp = new ALipayConfig();

    /**
     * 雪花算法
     */
    SnowFlake sf = new SnowFlake(1,1);

    /**
     * Vo层
     */
    KryptonVO kv = new KryptonVO();

    /**
     * 充值的时候最迟的交易时长
     */
    private static final String timeout_express = "15m";

    /**
     * 交易的关键字 / 付款方姓名
     */
    private static final String subject = "存钱罐";

    /**
     * 对一笔订单的描述
     */
    private static final String body = "存钱罐充值";

    /**
     * 收款方账户类型
     */
    private static final String payee_type = "ALIPAY_LOGONID";

    /**
     * 收款账号
     */
//    private static final String payee_account = "cytedp3783@sandbox.com";

    /**
     * 放款人姓名
     */
    private static final String payer_show_name = "cytedp3783";

    /**
     * 收款人真实姓名
     */
//    private static final String payee_real_name = "存钱罐提现";

    /**
     * app充值/支付
     * @return
     */
    public String pay(BigDecimal moeny) throws AlipayApiException {
        AlipayClient alipayClient = new DefaultAlipayClient(alp.gatewayUrl, alp.app_id, alp.merchant_private_key
                , "json", alp.charset, alp.alipay_public_key, alp.sign_type);

        AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
        Map<String, Object> map = new HashMap<>();
        map.put("timeout_express",timeout_express); // 最晚交易完成时间
        map.put("total_amount",moeny); // 订单总余额
        map.put("body",body); // 对一笔订单的描述
        map.put("subject",subject); // 商品标题/交易标题/订单标题/订单关键字
        map.put("out_trade_no",sf.nextId()); // 商户订单号  / 雪花算法
        request.setBizContent(JSON.toJSONString(map));
        AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
        if(response.isSuccess()){
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
        // 提交表单
        String submitFormData = response.getBody();

        return submitFormData;
    }

    /**
     * app提现/转账
     * @return
     * @throws AlipayApiException
     */
    public String Transfer(BigDecimal moeny,String payee_account) throws AlipayApiException {
        AlipayClient alipayClient = new DefaultAlipayClient(alp.gatewayUrl, alp.app_id, alp.merchant_private_key,
                "json", alp.charset, alp.alipay_public_key, alp.sign_type);
        AlipayFundTransToaccountTransferRequest request = new AlipayFundTransToaccountTransferRequest();
        Map<String, Object> map = new HashMap<>();
        map.put("out_biz_no", sf.nextId()); // 订单号  用雪花算法
        map.put("payee_type", payee_type); // 收款方账户类型 ALIPAY_USERID：支付宝账号对应的支付宝唯一用户号。以2088开头的16位纯数字组成 ALIPAY_LOGONID：支付宝登录号，支持邮箱和手机号格式。
        map.put("payee_account", payee_account);  // 支付宝沙箱账号 ---- 收款账号 前端传回来的绑定好的支付宝
        map.put("amount", moeny);  // 金额  前端传回来的要提现的金额
        map.put("payer_show_name", payer_show_name);    // 放款人姓名
//        map.put("payee_real_name",subject); // 收款人姓名
        request.setBizContent(JSON.toJSONString(map));
        AlipayFundTransToaccountTransferResponse from = alipayClient.execute(request);
        if(from.isSuccess()){
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
        String submitFormData = from.getBody();

        return submitFormData;
    }

}
